package com.ruoyi.basic.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.domain.BasicWork;
import com.ruoyi.basic.service.IBasicUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.BasicSalary;
import com.ruoyi.basic.service.IBasicSalaryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 岗位工资Controller
 * 
 * @author ruoyi
 * @date 2023-01-17
 */
@Controller
@RequestMapping("/basic/salary")
public class BasicSalaryController extends BaseController
{
    private String prefix = "basic/salary";

    @Autowired
    private IBasicSalaryService basicSalaryService;


    @RequiresPermissions("basic:salary:view")
    @GetMapping("/{salaryType}")
    public String salary(@PathVariable("salaryType") String salaryType, ModelMap mmap)
    {
        if(ShiroUtils.getSysUser().isAdmin() || ShiroUtils.getSubject().hasRole("paymentmanager") || ShiroUtils.getSubject().hasRole("administrator")){
            mmap.put("showFlag", true);
        }else{
            mmap.put("showFlag", false);
        }
        mmap.put("salaryType", salaryType);
        return prefix + "/salary";
    }

    /**
     * 查询岗位工资列表
     */
    @RequiresPermissions("basic:salary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicSalary basicSalary)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin() && !ShiroUtils.getSubject().hasRole("paymentmanager") && !ShiroUtils.getSubject().hasRole("administrator")){
            basicSalary.setIdCard(ShiroUtils.getLoginName());
        }
        List<BasicSalary> list = basicSalaryService.selectBasicSalaryList(basicSalary);
        return getDataTable(list);
    }

    /**
     * 导出岗位工资列表
     */
    @RequiresPermissions("basic:salary:export")
    @Log(title = "员工工资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasicSalary basicSalary)
    {
        List<BasicSalary> list = basicSalaryService.selectBasicSalaryList(basicSalary);
        ExcelUtil<BasicSalary> util = new ExcelUtil<BasicSalary>(BasicSalary.class);
        return util.exportExcel(list, "工资数据");
    }

    /**
     * 导入岗位工资
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "员工工资", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:salary:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, String salaryType, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicSalary> util = new ExcelUtil<BasicSalary>(BasicSalary.class);
        List<BasicSalary> salaryList = util.importExcel(file.getInputStream());
        String message = basicSalaryService.importSalary(salaryList, salaryType, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("basic:salary:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicSalary> util = new ExcelUtil<BasicSalary>(BasicSalary.class);
        return util.importTemplateExcel("员工工资信息");
    }

    /**
     * 新增岗位工资
     */
    @GetMapping("/add/{salaryType}")
    public String add(@PathVariable("salaryType") String salaryType, ModelMap mmap)
    {
        mmap.put("salaryType", salaryType);
        return prefix + "/add";
    }

    /**
     * 新增保存岗位工资
     */
    @RequiresPermissions("basic:salary:add")
    @Log(title = "员工工资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicSalary basicSalary)
    {
        basicSalary.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicSalaryService.insertBasicSalary(basicSalary));
    }

    /**
     * 修改岗位工资
     */
    @GetMapping("/edit/{salaryId}")
    public String edit(@PathVariable("salaryId") Long salaryId, ModelMap mmap)
    {
        BasicSalary basicSalary = basicSalaryService.selectBasicSalaryBySalaryId(salaryId);
        mmap.put("basicSalary", basicSalary);
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位工资
     */
    @RequiresPermissions("basic:salary:edit")
    @Log(title = "员工工资", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicSalary basicSalary)
    {
        basicSalary.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicSalaryService.updateBasicSalary(basicSalary));
    }

    /**
     * 删除岗位工资
     */
    @RequiresPermissions("basic:salary:remove")
    @Log(title = "员工工资", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basicSalaryService.deleteBasicSalaryBySalaryIds(ids));
    }
}
